//
//  UIButton+QTAdd.m
//  Pods
//
//  Created by 张俊博 on 16/8/23.
//
//

#import "UIButton+QTAdd.h"
#import "UIColor+QTAdd.h"
#import "UIImage+QTAdd.h"
#import <objc/runtime.h>
#import "QTCategoryMacro.h"

static CGFloat const DEFAULT_SPACING = 6.0f;

QTSYNTH_DUMMY_CLASS(UIButton_QTAdd)

@implementation UIButton (QTAdd)

+ (void)load {
    //交换 setHighlighted:, setEnabled: 和 setSelected: 方法
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        Class aClass = [self class];
//        method_exchangeImplementations(class_getInstanceMethod(aClass, @selector(setHighlighted:)),
//                                       class_getInstanceMethod(aClass, @selector(sl_setHighlighted:))
//                                       );
//        method_exchangeImplementations(class_getInstanceMethod(aClass, @selector(setEnabled:)),
//                                       class_getInstanceMethod(aClass, @selector(sl_setEnabled:))
//                                       );
//        method_exchangeImplementations(class_getInstanceMethod(aClass, @selector(setSelected:)),
//                                       class_getInstanceMethod(aClass, @selector(sl_setSelected:))
//                                       );
//    });
}

#pragma mark - public method

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {
//    if (backgroundColor) {
//        [self.backgroundColors setObject:backgroundColor forKey:@(state)];
//    }
//    if(self.state == state) {
//        self.backgroundColor = backgroundColor;
//    }
    
    [self setBackgroundImage:[UIImage imageWithColor:backgroundColor] forState:state];
}

//- (UIColor *)backgroundColorForState:(UIControlState)state {
//    return [self.backgroundColors objectForKey:@(state)];
//}
//
//#pragma mark - override
//
//- (void)sl_setSelected:(BOOL)selected {
//    [self sl_setSelected:selected];
//    
//    [self _update];
//}
//
//- (void)sl_setEnabled:(BOOL)enabled {
//    [self sl_setEnabled:enabled];
//    
//    [self _update];
//}
//
//- (void)sl_setHighlighted:(BOOL)highlighted {
//    [self sl_setHighlighted:highlighted];
//    
//    [self _update];
//}
//
//#pragma mark - private method
//
//- (void)_update {
//    UIColor *backgroundColor = [self backgroundColorForState:self.state];
//    if (backgroundColor) {
//        self.backgroundColor = backgroundColor?backgroundColor:self.backgroundColor;
//    } else {
//        UIColor *normalColor = [self backgroundColorForState:UIControlStateNormal];
//        self.backgroundColor = normalColor?normalColor:self.backgroundColor;
//    }
//}
//
//#pragma mark - setter and getter
//
////backgroundColors
//- (void)setBackgroundColors:(NSMutableDictionary *)backgroundColors {
//    objc_setAssociatedObject(self, @selector(backgroundColors), backgroundColors, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//}
//
//- (NSMutableDictionary *)backgroundColors {
//    NSMutableDictionary *_backgroundColors = objc_getAssociatedObject(self, @selector(backgroundColors));
//    if(!_backgroundColors) {
//        _backgroundColors = [[NSMutableDictionary alloc] init];
//        self.backgroundColors = _backgroundColors;
//    }
//    return _backgroundColors;
//}

#pragma mark - Bootstrap

-(void)bootstrapStyle{
    self.layer.borderWidth = 0.5;
    self.layer.cornerRadius = 3.0;
    self.layer.masksToBounds = YES;
    [self setAdjustsImageWhenHighlighted:NO];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //[self.titleLabel setFont:[UIFont fontWithName:@"FontAwesome" size:self.titleLabel.font.pointSize]];
}

-(void)loginStyle{
    [self bootstrapStyle];
    self.backgroundColor = [UIColor colorWithRed:252.0/255.0 green:114.0/255.0 blue:86.0/255.0 alpha:1];
    self.layer.borderColor = [[UIColor colorWithRed:252.0/255.0 green:114.0/255.0 blue:86.0/255.0 alpha:0] CGColor];
    [self setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:200/255.0 green:100/255.0 blue:100/255.0 alpha:1] size:self.bounds.size] forState:UIControlStateHighlighted];
}
-(void)profileStyle{
    [self bootstrapStyle];
    self.backgroundColor = [UIColor whiteColor];
    self.layer.borderColor = [[UIColor clearColor] CGColor];
}
- (void)updateDownApperance:(UIButton*)sender
{
    self.layer.borderColor = [UIColorHex(0xfc7265) CGColor];
}

- (void)updateCancelApperance:(UIButton*)sender
{
    self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
}

#pragma mark - Customer Layout

- (void)verticalCenterImageAndTitle:(CGFloat)spacing
{
//    CGSize imageSize = self.imageView.frame.size;
//    CGSize titleSize = self.titleLabel.frame.size;
//    
//    self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing/2), 0.0);
//    
//    titleSize = self.titleLabel.frame.size;
//    
//    self.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing/2), 0.0, 0.0, - titleSize.width);

    
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
//    CGSize textSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font];
    CGSize textSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
}

- (void)verticalCenterImageAndTitle
{
    [self verticalCenterImageAndTitle:DEFAULT_SPACING];
}

- (void)horizontalCenterTitleAndImage:(CGFloat)spacing
{
    CGSize imageSize = self.imageView.frame.size;
//    CGSize titleSize = self.titleLabel.frame.size;
    
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, 0.0, imageSize.width + spacing/2);
    
    CGSize titleSize = self.titleLabel.frame.size;
    
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, titleSize.width + spacing/2, 0.0, - titleSize.width);
}

- (void)horizontalCenterTitleAndImage
{
    [self horizontalCenterTitleAndImage:DEFAULT_SPACING];
}

- (void)horizontalCenterImageAndTitle:(CGFloat)spacing;
{
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0,  0.0, 0.0,  - spacing/2);
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, - spacing/2, 0.0, 0.0);
}

- (void)horizontalCenterImageAndTitle;
{
    [self horizontalCenterImageAndTitle:DEFAULT_SPACING];
}


- (void)horizontalCenterTitleAndImageLeft:(CGFloat)spacing
{
//    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, - spacing, 0.0, 0.0);
    CGSize imageSize = self.imageView.frame.size;
//    CGSize titleSize = self.titleLabel.frame.size;
    
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, 0.0, 0.0);
    
//    CGSize titleSize = self.titleLabel.frame.size;
    
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, - spacing, 0.0,  imageSize.width);
}

- (void)horizontalCenterTitleAndImageLeft
{
    [self horizontalCenterTitleAndImageLeft:DEFAULT_SPACING];
}

- (void)horizontalCenterTitleAndImageRight:(CGFloat)spacing
{
    CGSize imageSize = self.imageView.frame.size;
//    CGSize titleSize = self.titleLabel.frame.size;
    
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, 0.0, 0.0);
    
    CGSize titleSize = self.titleLabel.frame.size;
    
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, titleSize.width + imageSize.width + spacing, 0.0, - titleSize.width);
}

- (void)horizontalCenterTitleAndImageRight:(CGFloat)spacing centerOffset:(CGFloat)offset
{
    CGSize imageSize = self.imageView.frame.size;
//    CGSize titleSize = self.titleLabel.frame.size;
    
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width + offset, 0.0, 0.0);
    
    CGSize titleSize = self.titleLabel.frame.size;
    
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, titleSize.width + imageSize.width + spacing + offset, 0.0, - titleSize.width);
}

- (void)horizontalCenterTitleAndImageRight
{
    [self horizontalCenterTitleAndImageRight:DEFAULT_SPACING];
}

@end
